package com.youthburin.rmp.services.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youthburin.base.domain.UserInfo;
import com.youthburin.base.services.AbstractService;
import com.youthburin.base.springjpa.MyRepository;
import com.youthburin.rmp.domain.RmpOpLog;
import com.youthburin.rmp.domain.RmpOpLogBusiType;
import com.youthburin.rmp.domain.RmpOpLogType;
import com.youthburin.rmp.repositories.RmpOpLogBusiTypeRepository;
import com.youthburin.rmp.repositories.RmpOpLogRepository;
import com.youthburin.rmp.repositories.RmpOpLogTypeRepository;
import com.youthburin.rmp.services.RmpOpLogService;

@Service("rmpOpLogService")
public class RmpOpLogServiceImpl extends AbstractService<RmpOpLog, String> implements RmpOpLogService {
	@Autowired
	private RmpOpLogRepository rmpOpLogRepository;
	@Autowired
	private RmpOpLogTypeRepository rmpOpLogTypeRepository;
	@Autowired
	private RmpOpLogBusiTypeRepository rmpOpLogBusiTypeRepository;

	@Override
	public MyRepository<RmpOpLog, String> getMyRepository() {
		return rmpOpLogRepository;
	}

	@Override
	public void addLog(String rmpOpLogType, String rmpOpLogBusiType) {
		addLog(rmpOpLogType, rmpOpLogBusiType, null, null, null);
	}

	@Override
	public void addLog(String rmpOpLogType, String rmpOpLogBusiType, String busiId) {
		addLog(rmpOpLogType, rmpOpLogBusiType, busiId, null, null);
	}

	@Override
	public void addLog(String rmpOpLogType, String rmpOpLogBusiType, String busiId, String busiName) {
		addLog(rmpOpLogType, rmpOpLogBusiType, busiId, busiName, null);
	}

	@Override
	public void addLog(String rmpOpLogType, String rmpOpLogBusiType, String busiId, String busiName, String logDesc) {
		RmpOpLog RmpOpLog = new RmpOpLog();
		RmpOpLog.setBusiId(busiId);
		RmpOpLog.setBusiName(busiName);
		RmpOpLog.setLogDesc(logDesc);

		UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();

		RmpOpLogType RmpOpLogTypeObj = rmpOpLogTypeRepository.findOne(StringUtils.isBlank(rmpOpLogType) ? "DEFAULT"
				: rmpOpLogType);
		RmpOpLogBusiType RmpOpLogBusiTypeObj = rmpOpLogBusiTypeRepository
				.findOne(StringUtils.isBlank(rmpOpLogBusiType) ? "DEFAULT" : rmpOpLogBusiType);

		RmpOpLog.setCreater(user.getCode());
		RmpOpLog.setIp(user.getIp());
		RmpOpLog.setCreateTime(new Date());
		RmpOpLog.setRmpOpLogType(RmpOpLogTypeObj);
		RmpOpLog.setRmpOpLogBusiType(RmpOpLogBusiTypeObj);

		rmpOpLogRepository.save(RmpOpLog);
	}
}
